$(function() {
    $('form').parsley();
    dynamicBinding();
    getAllExercise();
});
//遍历所有Exercise输出到表格
function getAllExercise() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {title: "序号", data: null,"render": function (data,type,row,meta) {
                return data=Number(meta.row)+Number(1);
            }},
        {title: "演习时间", data: "exerciseTime"},
        {title: "演习要求", data: "exerciseRequirement"},
        {title: "演习地点", data: "exercisePosition"},
        {title: "演习名称", data: "exerciseName"},
        {title: "演习内容", data: "exerciseContent"},
        {title: "演习单位", data: "exerciseUnit"},
        {title: "操作", data: "id","orderable": false, // 禁用排序
            "defaultContent": "","render": function (data,type,row,meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateExerciseModal'>更新</button>";
                }
                return data = deleteStr+updateStr;}}
    ];
    let url ='exercise/getAllExercise';
    dataTableInit(url,columns);
}
//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/exercise/deleteExercise');
    //为所有的update按钮绑定update事件
    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $('#toUpdateExercise'+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        $('#toUpdateExercise').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateExercise')[0]);
            data.set("id", id);
            data.set("exerciseTime",Date($(" #updateExerciseTime").val()));
            updateAjax(data, '/exercise/updateExercise', false, false, "#updateExerciseModal");
            return false;
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddExercise').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddExercise')[0]);
        data.set("exerciseTime",Date($(" #addExerciseTime").val()));
        addAjax(data, '/exercise/addExercise', false, false, "#addExerciseModal");
        return false;
    });

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addExerciseModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddExercise").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateExerciseModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateExercise").reset();
    });











   /*
    //为所有的update按钮绑定update事件
    $("#exercise-table-body").on('click', ".data-update", function(){
        console.log("updateExercise被点击");
        let _id=$(this).attr('data-id');

        $("#toUpdateExercise").ajaxForm(function () {
            let id = _id;
            let exerciseTime = new Date($(" #updateExerciseTime ").val());
            let exerciseRequirement = $(" #updateExerciseRequirement ").val();
            let exercisePosition = $(" #updateExercisePosition ").val();
            let exerciseName = $(" #updateExerciseName ").val();
            let exerciseContent = $(" #updateExerciseContent ").val();
            let exerciseUnit = $(" #updateExerciseUnit ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/exercise/updateExercise',
                data: { id: id,exerciseTime:exerciseTime,exerciseRequirement:exerciseRequirement,
                    exercisePosition:exercisePosition,exerciseName:exerciseName,exerciseContent:exerciseContent,exerciseUnit:exerciseUnit},
                success: function (data) {
                    if (data.code==200){
                        window.location.reload();
                    }else {
                        console.log(data.msg);
                    }
                },
                error: function (data) {
                }
            })
        });
    });
    //为add按钮绑定add事件
    $("#toAddExercise").on('click', function(){
        $("#toAddExercise").ajaxForm(function () {
            let exerciseTime = new Date($(" #addExerciseTime ").val());
            let exerciseRequirement = $(" #addExerciseRequirement ").val();
            let exercisePosition = $(" #addExercisePosition ").val();
            let exerciseName = $(" #addExerciseName ").val();
            let exerciseContent = $(" #addExerciseContent ").val();
            let exerciseUnit = $(" #addExerciseUnit ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/exercise/addExercise',
                data: { exerciseTime:exerciseTime,exerciseRequirement:exerciseRequirement,
                    exercisePosition:exercisePosition,exerciseName:exerciseName,exerciseContent:exerciseContent,exerciseUnit:exerciseUnit},
                success: function (data) {
                    if(data.code==200){
                        window.location.reload();
                    }else{
                        console.log(data.msg);
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            })
        });
    });*/
}
